var api = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        navArr: [{
            name: "推荐",
            id: "0"
        }, {
            name: "关注",
            id: "1"
        }],
        activeNav: "0",
        page: 1,
        recomList: [],
        fowllData: [],
        recomUserList: [],
        attentionArticleList: [],
        artId: [],
        upLoading: true, 
        fowLoading: true,
        isAttention: '',
        isRefresh: !0

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var page = this.data.page, u_id = wx.getStorageSync('userData').u_id;
        this.recomLoading(u_id, page);
    },
    // 推荐加载的页面
    recomLoading: function(u_id, page){
        var a = this;
        wx.showLoading({ title: "加载中" });
        wx.request({
            url: api.globalData.Api + '/recomList',
            method: 'POST',
            header: getApp().header,
            data: {
                u_id: u_id,
                page: page
            },
            success: function (res) {
                if (res.data.status == 1) {
                    var r = res.data.data;
                    a.data.recomList = r;
                    a.data.artId = [];
                    for (let i = 0; i < r.length; i++){
                        a.data.artId.push(r[i].id);
                    }
                    a.setData({
                        recomData: r
                    }), wx.hideLoading();
                }
            },
        })
    },
    //  关注加载页面
    fowllLoading: function (u_id, page){
        var a = this;
        wx.showLoading({
            title: '玩命加载中...',
        });
        wx.request({
            url: api.globalData.Api + '/attentionList',
            method: 'GET',
            data: {
                userId: u_id,
                page: page
            },
            success: function (res) {
                if (res.data.status == 1) {
                    var e = a.data.recomData, t = a.data.attentionArticleList, q = res.data.data.recomUserList;
                    if (page > 1) {
                        if (res.data.data.attentionArticleList !== null) {
                            if (res.data.data.attentionArticleList.data.length == 0) {
                                wx.showToast({
                                    title: '已全部加载完成'
                                });
                            } else {
                                a.setData({
                                    fowllData: res.data.data,
                                    recomUserList: a.data.recomUserList,
                                    attenData: t.concat(res.data.data.attentionArticleList.data)
                                }), wx.hideLoading(), a.data.page++;
                            }
                        } else {
                            a.setData({
                                isRefresh: !1
                            }), wx.showToast({ title: '已全部加载完成' });
                        }
                    } else {
                        for (let i = 0; i < q.length; i++) { q[i].status = 0 };
                        a.data.recomUserList = q;
                        let r = res.data.data;
                        if (r.attentionArticleList == null) {
                            a.setData({
                                fowllData: r,
                                recomUserList: a.data.recomUserList,
                                isRefresh: !1
                            }), wx.hideLoading();
                        } else {
                            a.data.attentionArticleList = r.attentionArticleList.data;
                            a.setData({
                                fowllData: r,
                                recomUserList: a.data.recomUserList,
                                attenData: a.data.attentionArticleList,
                                isRefresh: !1
                            }), wx.hideLoading();
                        }
                    }
                }
            }
        })   
        
    },
    switchTab: function(t) {
        var nav_id = t.currentTarget.dataset.id, a = this;
        a.setData({
            activeNav: nav_id
        });
    },
    // 推荐和关注切换
    changePage: function (t) {
        var a = this, e = t.detail.current.toString(), u_id = wx.getStorageSync('userData').u_id;           a.data.page = 1;
        if(e == 1){ 
            a.fowllLoading(u_id, a.data.page)
        }else{ 
            a.recomLoading(u_id, a.data.page) 
        }
        a.setData({  activeNav: e  });
    },
    // 上拉加载
    refreshData: function(){
        var a = this, u_id = wx.getStorageSync('userData').u_id, page = a.data.page + 1;
        if (a.data.activeNav == 0){
            wx.showLoading({
                title: '玩命加载中...',
            });
            wx.request({
                url: api.globalData.Api + '/recomList',
                method: 'POST',
                header: getApp().header,
                data: {
                    page: page,
                    a_ids: a.data.artId
                },
                success: function (res) {
                    if (res.data.status == 1) {
                        var r = res.data.data;
                        if (r.length == 0) {
                            wx.showToast({
                                title: '已全部加载完成'
                            });
                        } else {
                            for (let i = 0; i < r.length; i++) { a.data.artId.push(r[i].id) }
                            a.setData({
                                recomData: a.data.recomList.concat(r),
                            }), wx.hideLoading(), a.data.page++;
                        }
                    }
                }
            }) 
        }else{
            a.fowllLoading(u_id, page);
        }
    },


    getFormId:function(e){
        var a = this, u = wx.getStorageSync('userData'), a_id = e.currentTarget.dataset.id;
        if (u.unionid && u.u_id){
            wx.navigateTo({
                url: '../article/article?article_id=' + a_id + '&user_id=' + u.u_id
            })
        }
        
    },
    toggleAttention(e){
        var a = this, o = e.currentTarget.dataset, u1_id = wx.getStorageSync('userData').u_id, u2_id = o.u2_id, i = o.index, s = o.status, d = a.data.recomUserList;
        wx.request({
            url: api.globalData.Api + '/followUser',
            method: "GET",
            data:{
                u1_id: u1_id,
                u2_id: u2_id,
            },
            dataType: 'json',
            success(res){
                if (res.data.status == 1){
                    if(s == 0){
                        d[i].status = 1;
                        wx.showToast({
                            title: '关注成功',
                        }),a.setData({
                            recomUserList: d
                        });
                    }else{
                        d[i].status = 0;
                        wx.showToast({
                            title: '取消关注成功',
                        }),a.setData({
                            recomUserList: d
                        });
                    }
                }
            }
        })
    },
    goMine: function(a){
        var u = a.currentTarget.dataset.u_id;
        wx.navigateTo({
            url: '../column/column?u_id=' + u
        })
    },
    changeRecomment : function(){
        var a = this, u_id = wx.getStorageSync('userData').u_id, page = a.data.page;
        a.fowllLoading(u_id, page), a.setData({ isRefresh: !0});
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },
    getFormIdAndNotNav: function(){
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})